package com.api.aop

import com.api.IFinal
import com.api.rpc.RPC_Service1_Feign
import com.base.model.service1.Model_User
import com.yfree.config.YConfig
import com.yfree.global.YController
import com.yfree.utils.YMD5Utils
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.context.event.ApplicationStartedEvent
import org.springframework.context.ApplicationListener
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Component
import java.util.*
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import kotlin.math.abs

@Component
class ApplicationStartAop(
    val service1: RPC_Service1_Feign
) : YController(), ApplicationListener<ApplicationStartedEvent> {
    @Value("\${spring.profiles.active}")
    private lateinit var active: String

    override fun onApplicationEvent(event: ApplicationStartedEvent) {
        YConfig.DEBUG = active != "release"
        YConfig.Response.successCode = 1
        YConfig.Request.filterCallBack = filterCallBack@{ request: HttpServletRequest, response: HttpServletResponse, handle: Any ->
            if (request.method == "GET") return@filterCallBack true
            // body
            val jkid = request.getParameter("jkid")
            val jkparam = request.getParameter("jkparam")
            // header
            val sign_channel = request.getHeader("sign-channel")
            val sign_device = request.getHeader("sign-device")
            val sign_version = request.getHeader("sign-version")
            val sign_rst = request.getHeader("sign-rst")
            val sign_uid = request.getHeader("sign-uid")
            val sign_password = request.getHeader("sign-password")
            val sign_sign = request.getHeader("sign-sign")
            if (!IFinal.noVerifyJkids.contains(jkid)) {
                if (sign_rst.isNullOrEmpty() || sign_uid.isNullOrEmpty() || sign_password.isNullOrEmpty() || sign_sign.isNullOrEmpty()) {
                    response.writer.println(yResult(code = -1001, message = "验签参数缺失"))
                    return@filterCallBack false
                }
                if (abs(Date().time - (sign_rst.toIntOrNull() ?: 0)) > 5000) {
                    response.writer.println(yResult(code = -1002, message = "时间戳不合法"))
                    return@filterCallBack false
                }
                val yModel = yFromRPC(service1.getTokenByUid(sign_uid), Model_User::class.java, throwWhenFail = false)
                if (yModel.root.code != HttpStatus.OK.value().toString()) {
                    response.writer.println(yResult(code = -1003, message = "token获取失败"))
                    return@filterCallBack false
                }
                if (sign_sign != YMD5Utils.getMD5("${YMD5Utils.getMD5(yModel.root.result.first().token)}$sign_rst")) {
                    response.writer.println(yResult(code = -1004, message = "sign校验失败"))
                    return@filterCallBack false
                }
            }
            request.setAttribute(
                IFinal.signKey, mapOf(
                    "channel" to sign_channel,
                    "device" to sign_device,
                    "version" to sign_version,
                    "uid" to sign_uid
                )
            )
            return@filterCallBack true
        }
    }
}
